.TH std::ranges::views::chunk,std::ranges::chunk_view 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::ranges::views::chunk,std::ranges::chunk_view \- std::ranges::views::chunk,std::ranges::chunk_view

.SH Synopsis
   Defined in header <ranges>
   template< ranges::view V >

       requires ranges::input_range<V>                           \fB(1)\fP (since C++23)
   class chunk_view

       : public ranges::view_interface<chunk_view<V>>
   template< ranges::view V >

       requires ranges::forward_range<V>                         \fB(2)\fP (since C++23)
   class chunk_view<V>

       : public ranges::view_interface<chunk_view<V>>
   namespace views {

       inline constexpr /* unspecified */ chunk = /* unspecified \fB(3)\fP (since C++23)
   */;

   }
   Call signature
   template< ranges::viewable_range R >
   constexpr ranges::view auto chunk( R&& r,                         (since C++23)
   ranges::range_difference_t<R> n );
   template< class DifferenceType >
   constexpr /*range adaptor closure*/ chunk( DifferenceType&& n     (since C++23)
   );
   Helper templates
   template< class I >                                           \fB(4)\fP (exposition only*)
   constexpr I __div_ceil( I num, I denom );

   chunk_view takes a view and a number n and produces a range of views (the chunks) of
   the original view, such that each chunk, except maybe the last one, has the size n.
   These chunks are non-overlapping, successive sub-ranges of the elements of the
   original view, in order.

   Let s be the size of the original view. If s is not the multiple of n, the size of
   the last produced view is exactly s % n (the remainder). Otherwise, the size of each
   chunk, including the last one, is n.

   The size of produced view is __div_ceil(s).

   If the n is not greater than 0 the behavior is undefined.

   1) An implementation that supports the underlying view V that models only
   input_range.
   2) A partial specialization that supports the underlying view V that models
   forward_range or stronger. Models common_range if the underlying view V is
   forward_range, common_range, and either sized_range or non bidirectional_range.
   3) The name views::chunk denotes a RangeAdaptorObject. Given subexpressions e and n,
   the expression views::chunk(e, n) is expression-equivalent to chunk_view(e, n).
   4) Computes the smallest integer value that is not less than the quotient of
   dividing num by denom. Equivalent to:

 I r = num / denom;
 if (num % denom)
     ++r;
 return r;

.SH Member functions

   constructor   constructs a chunk_view
   (C++23)       \fI(public member function)\fP
   base          returns a copy of the underlying (adapted) view
   (C++23)       \fI(public member function)\fP
   begin         returns an iterator to the beginning
   (C++23)       \fI(public member function)\fP
   end           returns an iterator or a sentinel to the end
   (C++23)       \fI(public member function)\fP
   size          returns the number of elements. Provided only if the underlying
   (C++23)       (adapted) range satisfies sized_range.
                 \fI(public member function)\fP
         Inherited from std::ranges::view_interface
   empty         returns whether the derived view is empty. Provided if it satisfies
   (C++20)       sized_range or forward_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   cbegin        returns a constant iterator to the beginning of the range.
   (C++23)       \fI(public member function of std::ranges::view_interface<D>)\fP
   cend          returns a sentinel for the constant iterator of the range.
   (C++23)       \fI(public member function of std::ranges::view_interface<D>)\fP
   operator bool returns whether the derived view is not empty. Provided if
   (C++20)       ranges::empty is applicable to it.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   front         returns the first element in the derived view. Provided if it
   (C++20)       satisfies forward_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   back          returns the last element in the derived view. Provided if it satisfies
   (C++20)       bidirectional_range and common_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   operator[]    returns the n^th element in the derived view. Provided if it satisfies
   (C++20)       random_access_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP

   Deduction guides

   Nested classes

   outer_iterator the output ("chunk-wise") iterator type when V models input_range \fB(1)\fP
   (C++23)        (exposition-only member class*)
   inner_iterator the inner ("element-wise") iterator type when V models input_range
   (C++23)        \fB(1)\fP
                  (exposition-only member class*)
   iterator       the iterator type when V models forward_range \fB(2)\fP
   (C++23)        (exposition-only member class template*)

   Helper templates

   template< class V >

   inline constexpr bool ranges::enable_borrowed_range<chunk_view<V>> =  (since C++23)

       ranges::forward_range<V> && ranges::enable_borrowed_range<V>;

   This specialization of ranges::enable_borrowed_range makes chunk_view satisfy
   borrowed_range when the underlying view V satisfies both, the forward_range and the
   borrowed_range.

.SH Notes

   If V models input_range \fB(1)\fP, chunk_view's iterator has a dedicated type:
   outer_iterator::value_type that is itself an input view.

   If V models forward_range or stronger \fB(2)\fP, chunk_view defers to views::take for its
   value_type.

   If V models bidirectional_range or stronger ranges \fB(2)\fP, the need to calculate size
   the last chunk correctly (from the end iterator) requires the underlying range type
   V to be sized_range.

     Feature-test macro    Value    Std           Feature
   __cpp_lib_ranges_chunk 202202L (C++23) std::ranges::chunk_view

.SH Example


// Run this code

 #include <algorithm>
 #include <iostream>
 #include <ranges>

 auto print_subrange = [](std::ranges::viewable_range auto&& r)
 {
     std::cout << '[';
     for (int pos{}; auto elem : r)
         std::cout << (pos++ ? " " : "") << elem;
     std::cout << "] ";
 };

 int main()
 {
     const auto v = {1, 2, 3, 4, 5, 6};

     for (const unsigned width : std::views::iota(1U, 2U + v.size()))
     {
         auto const chunks = v | std::views::chunk(width);
         std::cout << "chunk(" << width << "): ";
         std::ranges::for_each(chunks, print_subrange);
         std::cout << '\\n';
     }
 }

.SH Output:

 chunk\fB(1)\fP: [1] [2] [3] [4] [5] [6]
 chunk\fB(2)\fP: [1 2] [3 4] [5 6]
 chunk\fB(3)\fP: [1 2 3] [4 5 6]
 chunk\fB(4)\fP: [1 2 3 4] [5 6]
 chunk\fB(5)\fP: [1 2 3 4 5] [6]
 chunk\fB(6)\fP: [1 2 3 4 5 6]
 chunk\fB(7)\fP: [1 2 3 4 5 6]

.SH References

     * C++23 standard (ISO/IEC 14882:2023):

     * 26.7.28 Chunk view [range.chunk]

.SH See also

   ranges::chunk_by_view splits the view into subranges between each pair of adjacent
   views::chunk_by       elements for which the given predicate returns false
   (C++23)               \fI(class template)\fP (range adaptor object)
   ranges::adjacent_view a view consisting of tuples of references to adjacent elements
   views::adjacent       of the adapted view
   (C++23)               \fI(class template)\fP (range adaptor object)
   ranges::slide_view    a view whose M^th element is a view over the M^th through (M +
   views::slide          N - 1)^th elements of another view
   (C++23)               \fI(class template)\fP (range adaptor object)
